﻿using System;
using System.Collections;
using System.Text.RegularExpressions;

namespace wojilu
{
    /// <summary>
    /// 常用类库
    /// </summary>
    public static class Utils
    {

        #region 取左、中、右
        /// <summary>
        /// 取左边的字符
        /// </summary>
        /// <param name="sSource">字符串</param>
        /// <param name="iLength">要取长度</param>
        /// <returns></returns>
        public static string Left(this string sSource, int iLength)
        {
            if (!sSource.StrIsNullOrEmpty())
            {
                if (iLength > sSource.Length)
                {
                    return sSource;
                }
                else
                {
                    return sSource.Substring(0, iLength);
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 取右边的字符
        /// </summary>
        /// <param name="sSource">字符串</param>
        /// <param name="iLength">要取长度</param>
        /// <returns></returns>
        public static string Right(this string sSource, int iLength)
        {
            if (!sSource.StrIsNullOrEmpty())
            {
                if (iLength > sSource.Length)
                {
                    return sSource;
                }
                else
                {
                    return sSource.Substring(sSource.Length - iLength, iLength);
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 取中间的字符。
        /// </summary>
        /// <param name="sSource">字符串</param>
        /// <param name="iStart">开始长度</param>
        /// <param name="iLength">结束长度</param>
        /// <returns></returns>
        public static string Mid(this string sSource, int iStart, int iLength)
        {
            if (!sSource.StrIsNullOrEmpty())
            {
                int iStartPoint = iStart > sSource.Length ? sSource.Length : iStart;
                return sSource.Substring(iStartPoint, iStartPoint + iLength > sSource.Length ? sSource.Length - iStartPoint : iLength);
            }
            return string.Empty;
        }

        #endregion

        #region 获取指定文件的扩展名
        /// <summary>
        /// 获取指定文件的扩展名
        /// </summary>
        /// <param name="fileName">指定文件名</param>
        /// <returns>扩展名</returns>
        public static string GetFileExtName(string fileName)
        {
            if (fileName.StrIsNullOrEmpty() || fileName.IndexOf('.') <= 0)
                return "";

            fileName = fileName.ToLower().Trim();
            return Right(fileName, fileName.Length - fileName.LastIndexOf('.') - 1);
        }
        #endregion

        #region 转换长整形数为文件尺寸文本
        /// <summary>
        /// 转换长整形数为文件尺寸文本
        /// </summary>
        /// <param name="Length">长整形数尺寸</param>
        /// <returns>转换后的尺寸文本</returns>
        public static string ToSize(long Length)
        {
            if (Length < 0x3e8L)
            {
                return (Length + " B");
            }
            long num = 0x400L;
            long num2 = 0xfa000L;
            if (Length < num2)
            {
                return (Math.Round((double)(((double)Length) / ((double)num)), 2) + " KB");
            }
            num *= 0x400L;
            num2 *= 0x400L;
            if (Length < num2)
            {
                return (Math.Round((double)(((double)Length) / ((double)num)), 2) + " MB");
            }
            num *= 0x400L;
            num2 *= 0x400L;
            if (Length < num2)
            {
                return (Math.Round((double)(((double)Length) / ((double)num)), 2) + " GB");
            }
            num *= 0x400L;
            return (Math.Round((double)(((double)Length) / ((double)num)), 2) + " TB");
        }

        #endregion

        #region 格式化占用空间大小的输出
        /// <summary>
        /// 格式化占用空间大小的输出
        /// </summary>
        /// <param name="size">大小</param>
        /// <returns></returns>
        public static string FormatNUM(long size)
        {
            decimal num;
            if (size > 0x40000000L)
            {
                num = Convert.ToDecimal(size) / Convert.ToDecimal(0x40000000);
                return (num.ToString("N") + " G");
            }
            if (size > 0x100000L)
            {
                num = Convert.ToDecimal(size) / Convert.ToDecimal(0x100000);
                return (num.ToString("N") + " M");
            }
            if (size > 0x400L)
            {
                num = Convert.ToDecimal(size) / Convert.ToDecimal(0x400);
                return (num.ToString("N") + " KB");
            }
            return (size + " 字节");
        }
        #endregion

        #region 判断是否手机访问
        /// <summary>
        /// 判断是否手机访问
        /// </summary>
        /// <returns></returns>
        public static bool IsMobile()
        {
            Hashtable ht = new Hashtable();
            ht["operaMobi"] = "Opera Mobi";
            ht["android"] = "android";
            ht["blackberry"] = "blackberry";
            ht["iphone"] = "(iphone|ipod)";
            ht["opera"] = "opera mini";
            ht["palm"] = "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)";
            ht["windows"] = "windows ce; (iemobile|ppc|smartphone|Windows Phone OS)";
            ht["generic"] = "(kindle|mobile|mmp|midp|o2|pda|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap)";
            if (!UrlOper.GetServerString("HTTP_X_WAP_PROFILE").StrIsNullOrEmpty() || !UrlOper.GetServerString("HTTP_PROFILE").StrIsNullOrEmpty())
            {
                return true;
            }
            else if (!UrlOper.GetServerString("HTTP_ACCEPT").StrIsNullOrEmpty() && (UrlOper.GetServerString("HTTP_ACCEPT").Contains("text/vnd.wap.wml") || UrlOper.GetServerString("HTTP_ACCEPT").Contains("application/vnd.wap.xhtml+xml")))
            {
                return true;
            }
            else
            {
                var useragent = UrlOper.GetServerString("HTTP_USER_AGENT");
                if (!useragent.StrIsNullOrEmpty())
                {
                    foreach (string key in ht.Keys)
                    {
                        if (Regex.IsMatch(useragent, ht[key].ToString(), RegexOptions.IgnoreCase))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        #endregion

    }
}
